﻿using System;

namespace CoreLibrary.Email.Enumeration {

    public class EmailState : IEquatable<EmailState> {

        public const int SENT       = 200;
        public const int FAILED     = 400;
        public const int REJECT     = 401;
        public const int DELAY      = 402;
        public const int SOFTBOUNCE = 404;
        public const int HARDBOUNCE = 405;
        public const int CANCELLED  = 700;
        public const int UNKNOWN    = 999;

        public static EmailState Sent       = new EmailState("Sent",        SENT);
        public static EmailState Failed     = new EmailState("Failed",      FAILED);
        public static EmailState Reject     = new EmailState("Reject",      REJECT);
        public static EmailState Delay      = new EmailState("Delay",       DELAY);
        public static EmailState SoftBounce = new EmailState("Soft Bounce", SOFTBOUNCE);
        public static EmailState HardBounce = new EmailState("Hard Bounce", HARDBOUNCE);
        public static EmailState Cancelled  = new EmailState("Cancelled",   CANCELLED);
        public static EmailState Unknown    = new EmailState("Unknown",     UNKNOWN);

        public string Name { get; set; }
        public int Status  { get; set; }

        public EmailState(string name, int status) {
            Name   = name;
            Status = status;
        }

        public bool Equals(EmailState other) {
            if (other == null) {
                return false;
            }

            if (ReferenceEquals(this, other)) {
                return true;
            }

            return Name.Equals(other.Name) && Status == other.Status;
        }

        public static EmailState ConvertMandrillStatus(string status) {
            switch (status.ToLower()) {
                case "sent"      : return Sent;
                case "queued"    : return Sent;
                case "scheduled" : return Sent;
                case "delay"     : return Delay;
                case "rejected"  : return Failed;
                case "invalid"   : return Failed;
                case "cancelled" : return Cancelled;
                default          : return Unknown;
            }
        }

        public static EmailState Convert(int state) {
            switch (state) {
                default : return Unknown;
            }
        }

    }

}